package designPatterns.behaviorMode.strategy;

/**
 * Created by thinkpad on 2015/8/15.
 * Strategy pattern is also called policy pattern.
 Here is a story about Strategy pattern.
 Suppose Mike sometimes speeds when driving,
 but he doesn't always do that. He may be stopped by a police officer.
 It's possible that the police is very nice,
 who would let him go without any ticket or with a simple warning. (Let call this kind of police "NicePolice".)
 Also it's possible that he may be caught by a hard police and gets a ticket. (Let's call this kind of police "HardPolice".)
 He doesn't know what kind of police would stop him, until he actually gets caught,
 that is, run-time. This is the whole point of Strategy pattern.
 */
interface Strategy {
    //defind a method for police to process speeding case.
    public void processSpeeding(int speed);
}

class NicePolice implements Strategy{
    @Override
    public void processSpeeding(int speed) {
        System.out.println("This is your first time, be sure don't do it again!");
    }
}
class HardPolice implements Strategy{
    @Override
    public void processSpeeding(int speed) {
        System.out.println("Your speed is "+ speed+ ", and should get a ticket!");
    }
}

class Situation {
    private Strategy strategy;

    public Situation(Strategy strategy){
        this.strategy = strategy;
    }

    public void handleByPolice(int speed){
        this.strategy.processSpeeding(speed);
    }
}

public class testStrategy {
    public static void main(String args[]){
        HardPolice hp = new HardPolice();
        NicePolice ep = new NicePolice();

        // In situation 1, a hard officer is met
        // In situation 2, a nice officer is met
        Situation s1 = new Situation(hp);
        Situation s2 = new Situation(ep);

        //the result based on the kind of police officer.
        s1.handleByPolice(10);
        s2.handleByPolice(10);
    }
}
